package thread;

import java.util.concurrent.CountDownLatch;

public class Demo32 {
    public static void main(String[] args) throws InterruptedException {
        //模拟跑步比赛
        //构造方法中设定有几个选手参赛
        CountDownLatch latch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(()->{
                try {
                    Thread.sleep(3000);
                    System.out.println("到达终点");
                    //countDown相当于“撞线”
                    latch.countDown();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            t.start();
            
        }
        //await 在等待所有的线程
        //调用countDown的次数达到初始化时的设定值
        //await 就返回，否则await就阻塞等待
        latch.await();
        System.out.println("比赛结束！");
    }
}
